#include "NetPointDevice.h"
#include "PointDevice.h"
#include "Command/PointProtocol/UdpPointSetSwitchCmd.h"
#include "File/Xml/OtherEquipmentXml.h"
#include "File/Ini/DeviceIni.h"
#include <QCoreApplication>
#include <QtMsgHandler>
#include "System/System.h"
NetPointDevice::NetPointDevice(QObject *parent) :
    BaseInterface<NetPointDevice>(parent)
{
     qDebug()<<"NetPointDevice runing-1";
    m_pInstance = this;
    netPointProtocol = new NetPointProtocol(this);
    pointProtocol = new PointProtocol(this);
    this->registerProtocol(netPointProtocol);
    this->registerProtocol(pointProtocol);

    //qDebug()<<"NetPoint runing";
    registerCmdHandler(NetControlPointCmd::FunctionCode,NetControlPointCmd::staticMetaObject, &NetPointDevice::controlPointCmdHandle);
    registerCmdHandler(NetElevatorAuthorizeCmd::FunctionCode,NetElevatorAuthorizeCmd::staticMetaObject, &NetPointDevice::elevatorAutiorizeCmdHandle);
    registerCmdHandler(NetElevatorToFirestCmd::FunctionCode,NetElevatorToFirestCmd::staticMetaObject, &NetPointDevice::elevatorToFirstCmdHandle);

    registerCmdHandler(NetElevatorCallsQueryCmd::FunctionCode,NetElevatorCallsQueryCmd::staticMetaObject, &NetPointDevice::netElevatorCallsQueryHandle);
    registerCmdHandler(NetElevatorCallsUPCmd::FunctionCode,NetElevatorCallsUPCmd::staticMetaObject, &NetPointDevice::netElevatorCallsUPHandle);
    registerCmdHandler(NetElevatorCallsDownCmd::FunctionCode,NetElevatorCallsDownCmd::staticMetaObject, &NetPointDevice::netElevatorCallsDOWMHandle);

    registerCmdHandler(NetElevatorBackDataCmd::FunctionCode,NetElevatorBackDataCmd::staticMetaObject, &NetPointDevice::netElevatorBackDataHandle);


   //registerCmdHandler(UdpPointRelay::FunctionCode,UdpPointRelay::staticMetaObject, &NetPointDevice::UdpPointRelayPost);
    registerFuncHandler(ControlPointFunc::FunctionCode, &NetPointDevice::controlPointFuncHandle);
    qDebug()<<"NetPointDevice runing-2";
}


void NetPointDevice::controlPointCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
    //qDebug()<<"runing controlPointCmdHandle";
    Q_UNUSED(source);
    NetControlPointCmd *controlPointCmd = qobject_cast<NetControlPointCmd*>(sharedCmd.data());

    int address = controlPointCmd->address();
    int value = controlPointCmd->value();

    ControlPointFunc *pointControlFunc = new ControlPointFunc();
    ControlPointStruct sourcePoint;
    sourcePoint.setAddress(address);
    sourcePoint.setValue(value);

    QList<ControlPointStruct> sourcePointList;
    sourcePointList.append(sourcePoint);
    pointControlFunc->setSourceDevice(this->name());
    pointControlFunc->setSourcePointList(sourcePointList);

    QSharedPointer<BaseFunc> sharedPointControlFunc(pointControlFunc);
    emit transferFuncSignal(sharedPointControlFunc);
}

void NetPointDevice::controlPointFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
    ControlPointFunc *pointControlFunc = qobject_cast<ControlPointFunc*>(sharedFunc.data());
    IPListXml *ipListXml = IPListXml::getInstance();

    QList<ControlPointStruct> controlPointList;
    controlPointList.append(pointControlFunc->sourcePointList());
    controlPointList.append(pointControlFunc->controlPointList());   
    if(controlPointList.isEmpty())
    {
        return;
    }

    QList<PortStruct> netList = ipListXml->netList();

    foreach (const PortStruct &net, netList)
    {
        PortStruct target;
        target.setType("UDP");
        target.setName("NetPointDeviceUDP");
        target.setIp(net.ip());
        target.setPort(net.port());

        foreach(const ControlPointStruct &controlPoint,controlPointList)
        {
            int address = controlPoint.address();
            int value = controlPoint.value();

            NetControlPointCmd *controlPointCmd = new NetControlPointCmd();

            controlPointCmd->setAddress(address);
            controlPointCmd->setValue(value);

            QSharedPointer<BaseCmd> sharedControlPointcmd(controlPointCmd);
            sendCmd(target,sharedControlPointcmd);

        }
    }
}

void NetPointDevice::elevatorAutiorizeCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
   // Q_UNUSED(source);

    ElevatorIPXml *elevatorIPXml =ElevatorIPXml::getInstance();
    QList<ElevatorStruct> structList;
    structList=elevatorIPXml->StructList();
    QString elevatorBrand = elevatorIPXml->getBrand();
    QString roomDir;
    if(elevatorBrand=="4") //奥的斯电梯  春和景明用
    {
        NetElevatorAuthorizeCmd *netElevatorAuthorizeCmd = qobject_cast<NetElevatorAuthorizeCmd*>(sharedCmd.data());
        QString extNumber = netElevatorAuthorizeCmd->doorExtNumber();//获取门口机的extNunber编号，来对应它所在楼层
        quint16 roomNumber = netElevatorAuthorizeCmd->roomNumber();//获取室内机
        QByteArray data;
        quint16 doorFloor;//门口机所在楼层，当extNumber为1表示在1楼，当extNumber为2表示在负一楼；当extNumber为3表示在负二楼 .... 依次下去
        quint8 doorDir=0; //门口机所在楼层电梯开门方向

        quint8 mantissa = roomNumber&0xff;  //获取四位房号的后两位
        if(mantissa !=elevatorIPXml->getMantissa().toInt())
        {

            return;
        }

        if(m_doorNumber == 57) //轿厢内二次确认门口机  编号9
        {
            doorFloor = 0;

            doorDir = 0;
        }

        if(m_doorNumber == 52) //-3楼门口机 编号4
        {
            doorFloor = 0x0301;

            doorDir = elevatorIPXml->getFB3Dir().toInt();
        }
        if(m_doorNumber == 51) //-2楼门口机 编号3
        {
            doorFloor = 0x0201;

            doorDir = elevatorIPXml->getFB2Dir().toInt();
        }
        if(m_doorNumber == 50) //-1楼门口机 编号2
        {
            doorFloor = 0x0101;

             doorDir = elevatorIPXml->getFB1Dir().toInt();
        }
        if(m_doorNumber == 49) //1楼门口机  编号1
        {
            doorFloor = 0x0100;

             doorDir = elevatorIPXml->getF1Dir().toInt();
        }

        data=netPointProtocol->generateAuthorizeData(roomNumber,doorFloor,doorDir,elevatorIPXml->getRoomDir().toInt(),"4");
        PortStruct target;
        foreach (const BasePort *port , m_portList)
        {

             if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
             {
                 target = port->portParam();
             }
        }
            qDebug()<<"elevator4 Autiorize send now";
         sendDataSignal(target,data);
         return;
    }
    if(elevatorBrand=="3") //日立电梯 当代璞玉项目
    {
        DeviceIni *deviceIni = DeviceIni::getInstance();
        ElevatorStruct structData=deviceIni->StructMap.value(source.ip());

         if(structData.getCenter1IP().remove(QRegExp("\\s"))==source.ip())
         {
           qDebug()<<"center1 ip";
                 return;  //保安中心的调梯命令不执行
         }

         if(structData.getCenter2IP().remove(QRegExp("\\s"))==source.ip())
         {
           qDebug()<<"center2 ip";
                 return;  //保安中心的调梯命令不执行
         }

        NetElevatorAuthorizeCmd *netElevatorAuthorizeCmd = qobject_cast<NetElevatorAuthorizeCmd*>(sharedCmd.data());
        QString extNumber = netElevatorAuthorizeCmd->doorExtNumber();//获取门口机的extNunber编号，来对应它所在楼层
        quint16 roomNumber = netElevatorAuthorizeCmd->roomNumber();//获取室内机
        QByteArray data;
        quint8 doorFloor;//门口机所在楼层，当extNumber为1表示在1楼，当extNumber为2表示在负一楼；同时，对于三菱电梯来说，负一的楼层编号是0
        if(m_doorNumber == 51) //-2楼门口机
            doorFloor = 1;
        if(m_doorNumber == 50) //-1楼门口机
            doorFloor = 2;
        if(m_doorNumber == 49) //1楼门口机
            doorFloor = 3;
        data=netPointProtocol->generateAuthorizeData(roomNumber,doorFloor,"3");
        PortStruct target;
        foreach (const BasePort *port , m_portList)
        {

             if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
             {
                 target = port->portParam();
             }
        }
            qDebug()<<"elevator3 Autiorize send now";
         sendDataSignal(target,data);
        return;
    }
    if(elevatorBrand=="2") // 三菱电梯
    {
        NetElevatorAuthorizeCmd *netElevatorAuthorizeCmd = qobject_cast<NetElevatorAuthorizeCmd*>(sharedCmd.data());
        QString extNumber = netElevatorAuthorizeCmd->doorExtNumber();//获取门口机的extNunber编号，来对应它所在楼层
        QString roomFloorNumber = netElevatorAuthorizeCmd->roomFloorNumber();//获取室内机所在楼层
        QByteArray data;
        QString doorDir; //门口机所在楼层电梯开门方向
         quint16 roomNumber = netElevatorAuthorizeCmd->roomNumber();//获取室内机房号
        quint8 doorFloor;//门口机所在楼层，当extNumber为1表示在1楼，当extNumber为2表示在负一楼；同时，对于三菱电梯来说，负一的楼层编号是0
        if(m_doorNumber == 49)    //1楼门口机
        {
            doorFloor = 1;
            doorDir = elevatorIPXml->getF1Dir();
        }
        if(m_doorNumber == 50)  //-1楼门口机
        {
            doorFloor = 0;
            doorDir = elevatorIPXml->getFB1Dir();
        }
        if(roomFloorNumber.toInt()==0)
            return;

        quint8 mantissa =roomNumber&0xff;  //获取四位房号的后两位
         QString roomDir;
        if(mantissa==1)
        {
            roomDir = elevatorIPXml->getM01Dir();
        }
        else if(mantissa==2)
        {
            roomDir = elevatorIPXml->getM02Dir();
        }
        else if(mantissa==3)
        {
            roomDir = elevatorIPXml->getM03Dir();
        }
        else if(mantissa==4)
        {
            roomDir = elevatorIPXml->getM04Dir();
        }


        data = netPointProtocol->generateAuthorizeData(roomNumber,doorFloor,doorDir.toInt(),roomDir.toInt(),"2");
      //  data=netPointProtocol->generateAuthorizeData(roomFloorNumber.toInt(),doorFloor,"2");
        PortStruct target;
        foreach (const BasePort *port , m_portList)
        {
             if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
             {
                 target = port->portParam();
             }
        }
            qDebug()<<"elevator2 Autiorize send now";
         sendDataSignal(target,data);
        return;
    }
    foreach(ElevatorStruct dataStruct,structList)
    {
        if(dataStruct.getIP()==source.ip())
        {
            quint16 roomNumber;
            QString floorNumber;
            QStringList floorNumberList;
            QByteArray data;
            bool ok=true;
            NetElevatorAuthorizeCmd *netElevatorAuthorizeCmd = qobject_cast<NetElevatorAuthorizeCmd*>(sharedCmd.data());
            roomNumber=netElevatorAuthorizeCmd->roomNumber();
            floorNumber = netElevatorAuthorizeCmd->floorNumber();
            floorNumberList = floorNumber.split(",");
            for(int i=0;i<floorNumberList.size();i++)
            {
                ok=true;
                if(floorNumberList.at(i).isEmpty())
                    continue;
                  char *s=floorNumberList.at(i).toUtf8().data();
                  for(int j=0;j<floorNumberList.at(i).length();j++){
                      if((*s <'0') || (*s >'9'))
                      {
                          ok=false;
                          break;
                      }
                      else
                          s++;
                }
                  if(ok==false)
                      continue;
                quint8 floor = floorNumberList.at(i).toInt();

            data=netPointProtocol->generateAuthorizeData(roomNumber,floor,"0");
            PortStruct target;
            foreach (const BasePort *port , m_portList)
            {
                 if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
                 {
                     target = port->portParam();
                 }
            }
            //qDebug()<<"elevator send now";

            sendDataSignal(target,data);

            QEventLoop loop;//定义一个新的事件循环
            QTimer::singleShot(500, &loop, SLOT(quit()));//创建单次定时器，槽函数为事件循环的退出函数
            loop.exec();
           }
            /*****************************************/
           /* QString QSDATA;
            QSDATA.append((roomNumber>>12)+0x30);
            QSDATA.append(((roomNumber&0xf00)>>8)+0x30);
            QSDATA.append(((roomNumber&0xf0)>>4)+0x30);
            QSDATA.append((roomNumber&0xf)+0x30);
            static QMutex mutex;
            mutex.lock();
            QString text;
            QString currentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
            text = "[2.2.15]"+currentTime +"-IP:"+source.ip()+ "-elevatorAutiorize-" +"RoomNum="+QSDATA+ "\r\n";
            QByteArray textArray = text.toLatin1();
            fprintf(stderr, textArray.constData());

            QFile file(QCoreApplication::applicationDirPath() + "/elevator.txt");
            file.open(QIODevice::WriteOnly | QIODevice::Append);
            QTextStream textStream(&file);
            textStream << text;
            file.flush();
            if(file.size()>0x100000)
            {
                file.remove();
            }
            file.close();

            mutex.unlock();*/
    /*****************************************/
        }
    }
}

void NetPointDevice::elevatorToFirstCmdHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
    //Q_UNUSED(sharedCmd);
    ElevatorIPXml *elevatorIPXml =ElevatorIPXml::getInstance();
    QList<ElevatorStruct> structList;
     QString elevatorBrand = elevatorIPXml->getBrand();
     if(elevatorBrand=="2") //三菱电梯
     {
         NetElevatorToFirestCmd *netElevatorToFirestCmd = qobject_cast<NetElevatorToFirestCmd*>(sharedCmd.data());
         m_doorNumber=netElevatorToFirestCmd->doorNumber();
         return;
     }

     if(elevatorBrand=="3") //日立电梯
     {
        NetElevatorToFirestCmd *netElevatorToFirestCmd = qobject_cast<NetElevatorToFirestCmd*>(sharedCmd.data());
        m_doorNumber=netElevatorToFirestCmd->doorNumber();
        return;
     }
     if(elevatorBrand=="4") //奥的斯电梯
     {
        NetElevatorToFirestCmd *netElevatorToFirestCmd = qobject_cast<NetElevatorToFirestCmd*>(sharedCmd.data());
        m_doorNumber=netElevatorToFirestCmd->doorNumber();
        return;
     }
    structList=elevatorIPXml->StructList();
    //qDebug()<<"To First Handle RUN";
    foreach(ElevatorStruct dataStruct,structList)
    {
        if(dataStruct.getIP()==source.ip())
        {
            QByteArray data;
            data=netPointProtocol->generateToFirstData(dataStruct.m_flootnum);
            qDebug()<<dataStruct.m_flootnum;
            PortStruct target;
            foreach (const BasePort *port , m_portList)
            {
                 if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
                 {
                     target = port->portParam();
                 }
            }
            qDebug()<<"first send now";
            sendDataSignal(target,data);
            /*****************************************/
           /* quint8 flootdata=dataStruct.m_flootnum;
            QString flootNum;
            flootNum.append((flootdata&0xf0>>4)+0x30);
            flootNum.append((flootdata&0xf)+0x30);
            static QMutex mutex;
            mutex.lock();
            QString text;
            QString currentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
            text = "[2.2.15]"+currentTime +"-IP:"+source.ip()+ "-elevatorToFirst" +"\r\n";
            QByteArray textArray = text.toLatin1();
            fprintf(stderr, textArray.constData());

            QFile file(QCoreApplication::applicationDirPath() + "/elevator.txt");
            file.open(QIODevice::WriteOnly | QIODevice::Append);
            QTextStream textStream(&file);
            textStream << text;
            file.flush();
            if(file.size()>0x100000)
            {
                file.remove();
            }
            file.close();

            mutex.unlock();*/
            /*****************************************/
        }
    }
}


void NetPointDevice::netElevatorCallsQueryHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(sharedCmd);
   // Q_UNUSED(source);
    DeviceIni *deviceIni = DeviceIni::getInstance();
    deviceIni->querydata.enqueue(source);
    QByteArray data;
    data=netPointProtocol->generateQueryData();
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
         {
             target = port->portParam();
         }
    }
     sendDataSignal(target,data);
}

void NetPointDevice::netElevatorCallsUPHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
   
    Q_UNUSED(sharedCmd);
    QByteArray data;
    ElevatorIPXml *elevatorIPXml =ElevatorIPXml::getInstance();
    QString doorZone=elevatorIPXml->getDoorZone();
    QString elevatorBrand=elevatorIPXml->getBrand();
    DeviceIni *deviceIni = DeviceIni::getInstance();
    ElevatorStruct structData=deviceIni->StructMap.value(source.ip());
    QString roomZone=structData.getRoomZone();
    qDebug()<<"netElevatorCallsUPHandle doorZone "<<doorZone<<" roomZone "<<roomZone;
       if((structData.getIP()==source.ip())&&(roomZone.contains(doorZone)))
       {
       quint16 roomNumData=structData.m_roomnum;
      // data=netPointProtocol->generateUPDOWMData(roomNumData,1);
       data=netPointProtocol->generateUPDOWMData(roomNumData,1,elevatorBrand);
       PortStruct target;
       foreach (const BasePort *port , m_portList)
       {
            if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
            {
                target = port->portParam();
            }
       }
       qDebug()<<"elevator send now";
       sendDataSignal(target,data);
       /*****************************************/
       QString QSDATA;
       QSDATA.append((roomNumData>>12)+0x30);
       QSDATA.append(((roomNumData&0xf00)>>8)+0x30);
       QSDATA.append(((roomNumData&0xf0)>>4)+0x30);
       QSDATA.append((roomNumData&0xf)+0x30);
       static QMutex mutex;
       mutex.lock();
       QString text;
       QString currentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
       text = "[2.2.15]"+currentTime +"-IP:"+source.ip()+ "-elevatorCallsUP" +"-" +"RoomNum="+QSDATA+ "\r\n";
       QByteArray textArray = text.toLatin1();
       fprintf(stderr, textArray.constData());

       QFile file(QCoreApplication::applicationDirPath() + "/elevator.txt");
       file.open(QIODevice::WriteOnly | QIODevice::Append);
       QTextStream textStream(&file);
       textStream << text;
       file.flush();
       if(file.size()>0x100000)
       {
           file.remove();
       }
       file.close();

       mutex.unlock();
       /*****************************************/
       }


}

void NetPointDevice::netElevatorCallsDOWMHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
   Q_UNUSED(sharedCmd);
    QByteArray data;
    ElevatorIPXml *elevatorIPXml =ElevatorIPXml::getInstance();
    QString doorZone=elevatorIPXml->getDoorZone();
    QString unitZone=elevatorIPXml->getUnitZone();
    QString elevatorBrand=elevatorIPXml->getBrand();
    if(elevatorBrand == "1")
    {
        QList<ElevatorStruct> structList;
        structList=elevatorIPXml->StructList();
        foreach(ElevatorStruct dataStruct,structList)
        {
            if(dataStruct.getIP()==source.ip())
            {

                data=netPointProtocol->generateUPDOWMData(0,0,elevatorBrand);
                PortStruct target;
                foreach (const BasePort *port , m_portList)
                {
                     if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
                     {
                         target = port->portParam();
                     }
                }
                qDebug()<<"elevator-1 send now";
                sendDataSignal(target,data);
            }
        }
        return;
    }
    else if(elevatorBrand == "2")
    {
        QList<ElevatorStruct> structList;
        structList=elevatorIPXml->StructList();


         qDebug()<<"source IP "<<source.ip();
        DeviceIni *deviceIni = DeviceIni::getInstance();
        ElevatorStruct structData=deviceIni->StructMap.value(source.ip());


        // QString zone = deviceIni->getZoneByIP(ip);

        QString roomZone=structData.getRoomZone();
        qDebug()<<"roomZone "<<roomZone<<" uintZone "<<unitZone;
       // if((structData.getIP().remove(QRegExp("\\s"))==source.ip())&&(roomZone.contains(unitZone)))
        if(roomZone.contains(unitZone))
        {
            QStringList list = roomZone.split("-");
            if(list.size()>0)
            {
                QString room = list.at(list.size()-1);
                 if(room.length() ==4)
                   {
                    quint16 floor = room.mid(0,2).toInt();
                    printf("floor is %d\r\n",floor);
                    QString mantissa =room.mid(2,2);  //获取四位房号的后两位
                     QString roomDir;
                    if(mantissa=="01")
                    {
                        roomDir = elevatorIPXml->getM01Dir();
                    }
                    else if(mantissa=="02")
                    {
                        roomDir = elevatorIPXml->getM02Dir();
                    }
                    else if(mantissa=="03")
                    {
                        roomDir = elevatorIPXml->getM03Dir();
                    }
                    else if(mantissa=="04")
                    {
                        roomDir = elevatorIPXml->getM04Dir();
                    }
                    else
                    {
                        return;
                    }
                    data=netPointProtocol->generateUPDOWMData(floor,roomDir.toInt(),elevatorBrand);

                    PortStruct target;
                    foreach (const BasePort *port , m_portList)
                    {
                         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
                         {
                             target = port->portParam();
                         }
                    }
                    qDebug()<<"elevator-2 send now";
                    sendDataSignal(target,data);
                }
            }
        }

        /*if(roomZone.contains(zone))
        {
            QStringList list = roomZone.split("-");
            if(list.size()>0)
            {
                bool ok;
                QString room = list.at(list.size()-1);
                qDebug()<<"room is "<<room;
                if(room.length() ==4)
                {
                QString mantissa =room.mid(2,2);  //获取四位房号的后两位
                if((mantissa ==elevatorIPXml->getMantissa())||(elevatorIPXml->getMantissa() == "00"))
                {
                    qDebug()<<"mantissa!!! "<<mantissa<<"--"<<elevatorIPXml->getRoomDir();
                     data=netPointProtocol->generateUPDOWMData(room.toInt(&ok,16),elevatorIPXml->getRoomDir().toInt(),elevatorBrand);
                     PortStruct target;
                     foreach (const BasePort *port , m_portList)
                     {
                          if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
                          {
                              target = port->portParam();
                          }
                     }
                     qDebug()<<"elevator-2 send now";
                     sendDataSignal(target,data);
                }

                }

            }
        }*/
           return;
    }
    else if(elevatorBrand=="3") //日立电梯
    {
         QString ip = System::ip();
         qDebug()<<"local ip is "<<ip;
         DeviceIni *deviceIni = DeviceIni::getInstance();
         QString zone = deviceIni->getZoneByIP(ip);
          qDebug()<<"local zone is "<<zone;
            ElevatorStruct structData=deviceIni->StructMap.value(source.ip());

          if(structData.getCenter1IP().remove(QRegExp("\\s"))==source.ip())
          {
            qDebug()<<"center1 ip";
                  return;
          }

          if(structData.getCenter2IP().remove(QRegExp("\\s"))==source.ip())
          {
            qDebug()<<"center2 ip";
                  return;
          }

          QString roomZone=structData.getRoomZone();
          if(roomZone.contains(zone))
          {
              QStringList list = roomZone.split("-");
              if(list.size()>0)
              {
                  bool ok;
                  QString room = list.at(list.size()-1);
                  qDebug()<<"room is "<<room;
                  data=netPointProtocol->generateUPDOWMData(room.toInt(&ok,16),0,elevatorBrand);

                  PortStruct target;
                  foreach (const BasePort *port , m_portList)
                  {
                       if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
                       {
                           target = port->portParam();
                       }
                  }
                  qDebug()<<"elevator-3 send now";
                  sendDataSignal(target,data);
              }
          }
          return;
    }
    else if(elevatorBrand=="4") //奥的斯电梯  春和景明用
    {
        QString ip = System::ip();
        qDebug()<<"local ip is "<<ip;
        DeviceIni *deviceIni = DeviceIni::getInstance();
        QString zone = deviceIni->getZoneByIP(ip);
        qDebug()<<"local zone is "<<zone;
        ElevatorStruct structData=deviceIni->StructMap.value(source.ip());

        QString roomZone=structData.getRoomZone();
        if(roomZone.contains(zone))
        {
            QStringList list = roomZone.split("-");
            if(list.size()>0)
            {
                bool ok;
                QString room = list.at(list.size()-1);
                qDebug()<<"room is "<<room;
                if(room.length() ==4)
                {
                QString mantissa =room.mid(2,2);  //获取四位房号的后两位
                if((mantissa ==elevatorIPXml->getMantissa())||(elevatorIPXml->getMantissa() == "00"))
                {
                    qDebug()<<"mantissa!!! "<<mantissa<<"--"<<elevatorIPXml->getRoomDir();
                     data=netPointProtocol->generateUPDOWMData(room.toInt(&ok,16),elevatorIPXml->getRoomDir().toInt(),elevatorBrand);
                     PortStruct target;
                     foreach (const BasePort *port , m_portList)
                     {
                          if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
                          {
                              target = port->portParam();
                          }
                     }
                     qDebug()<<"elevator-4 send now";
                     sendDataSignal(target,data);
                }

                }

            }
        }
        return;
    }
    DeviceIni *deviceIni = DeviceIni::getInstance();
    ElevatorStruct structData=deviceIni->StructMap.value(source.ip());
    QString roomZone=structData.getRoomZone();
       qDebug()<<"netElevatorCallsDOWMHandle doorZone "<<doorZone<<" roomZone "<<roomZone;
       if((structData.getIP().remove(QRegExp("\\s"))==source.ip())&&(roomZone.contains(doorZone)))
       {
       quint16 roomNumData=structData.m_roomnum;
       //data=netPointProtocol->generateUPDOWMData(roomNumData,0);
        data=netPointProtocol->generateUPDOWMData(roomNumData,0,elevatorBrand);
       PortStruct target;
       foreach (const BasePort *port , m_portList)
       {
            if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
            {
                target = port->portParam();
            }
       }
       qDebug()<<"elevator send now";
       sendDataSignal(target,data);
       /*****************************************/
       QString QSDATA;
       QSDATA.append((roomNumData>>12)+0x30);
       QSDATA.append(((roomNumData&0xf00)>>8)+0x30);
       QSDATA.append(((roomNumData&0xf0)>>4)+0x30);
       QSDATA.append((roomNumData&0xf)+0x30);
       static QMutex mutex;
       mutex.lock();
       QString text;
       QString currentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
       text = "[2.2.15]"+currentTime +"-IP:"+source.ip()+ "-elevatorCallsDOWM" +"-" +"RoomNum="+QSDATA+ "\r\n";
       QByteArray textArray = text.toLatin1();
       fprintf(stderr, textArray.constData());

       QFile file(QCoreApplication::applicationDirPath() + "/elevator.txt");
       file.open(QIODevice::WriteOnly | QIODevice::Append);
       QTextStream textStream(&file);
       textStream << text;
       file.flush();
       if(file.size()>0x100000)
       {
           file.remove();
       }
       file.close();

       mutex.unlock();
       /*****************************************/
       }


}


void NetPointDevice::netElevatorBackDataHandle(PortStruct source,QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    qDebug()<<"hello my friend back";
     NetElevatorBackDataCmd *backDataCmd = qobject_cast<NetElevatorBackDataCmd*>(sharedCmd.data());
     qDebug()<<"Elevatorfloor="<<backDataCmd->Elevatorfloor();
     qDebug()<<"Elevatornum="<<backDataCmd->Elevatornum();
     qDebug()<<"Elevatorstate="<<backDataCmd->Elevatorstate();
     DeviceIni *deviceIni = DeviceIni::getInstance();
    while(!deviceIni->querydata.isEmpty())
    {

        PortStruct BackPort= deviceIni->querydata.dequeue();
        QByteArray data;
        QByteArray dataHand;
        QByteArray floorData;
        QByteArray elevatorData;
        QByteArray elevatorState;
        QString floor;
        QString lifeData;

        floor=QString::number(backDataCmd->Elevatorfloor(),10);

        dataHand.append("ReturnLiftStatus\r\n");
        floorData.append("floor:");
        floorData.append(floor);
        floorData.append("\r\n");

        elevatorData.append("liftNum:");
        lifeData=QString::number(backDataCmd->Elevatornum(),10);
        elevatorData.append(lifeData);
        elevatorData.append("\r\n");
        elevatorState.append("scheduleType:");

        quint8 state;
        switch (backDataCmd->Elevatorstate()) {
        case 0:state=0x32;

            break;
        case 1:state=0x31;

            break;
        case 2:state=0x33;

            break;
        default:state=0x30;
            break;
        }

        elevatorState.append(state);
        elevatorState.append("\r\n");

        data.append(dataHand);
        data.append(floorData);
        data.append(elevatorData);
        data.append(elevatorState);

        char *dataa=data.data();
        qDebug()<<dataa;

        sendDataSignal(BackPort,data);
    }
    qDebug()<<"runtting BackData";
}
